package com.meichis.yndms.common;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.meichis.yndms.R;


public class MapsImpl implements AMap.InfoWindowAdapter, AMapLocationListener, LocationSource {
    private static MapsImpl INSTANCE;

    private Context context;
    private AMap aMap;
    private MyLocationStyle myLocationStyle;
    /*  激活定位 */
    private OnLocationChangedListener mListener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    View infoWindow = null;
    private LonLatListener listener;
    private Marker curShowWindowMarker;


    /**
     * @return
     */
    public static MapsImpl getInstance() {
        if (INSTANCE == null) {
            synchronized (MapsImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new MapsImpl();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 定位，获得坐标
     *
     * @param AMap
     * @param Context
     */
    public void initMap(AMap AMap, Context Context, LonLatListener Listener) {
        this.aMap = AMap;
        this.context = Context;
        this.listener = Listener;
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        //初始化定位蓝点样式类
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);//连续定位、蓝点不会移动到地图中心点，地图依照设备方向旋转，并且蓝点会跟随设备移动。
        // 设置圆形的边框颜色
        myLocationStyle.strokeColor(Color.TRANSPARENT);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(Color.TRANSPARENT);
        myLocationStyle.interval(1000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
//        // 设置定位监听
        aMap.setLocationSource(this);
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
        aMap.setOnMapClickListener((LatLng latLng) -> {
            //点击其它地方隐藏infoWindow
            if (curShowWindowMarker != null) {
                curShowWindowMarker.hideInfoWindow();
            }
        });
        aMap.setOnMarkerClickListener(marker -> {
            if (curShowWindowMarker != null) {
                curShowWindowMarker.hideInfoWindow();
            }
            curShowWindowMarker = marker;
            return false;
        });
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (listener != null) {
            listener.onLocationChanged(aMapLocation);
        }
        if (mListener != null && aMapLocation != null) {

            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }


    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mlocationClient == null) {
            //初始化定位
            mlocationClient = new AMapLocationClient(context);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位回调监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();//启动定位
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }


    @Override
    public View getInfoWindow(Marker marker) {
        if (infoWindow == null) {
            infoWindow = LayoutInflater.from(context).inflate(
                    R.layout.custom_info_window, null);
        }
        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定义infowinfow窗口
     */
    public void render(Marker marker, View view) {
        //如果想修改自定义Infow中内容，请通过view找到它并修改
        if (marker.getTitle().length() > 0) {
            ((TextView) view.findViewById(R.id.title)).setText(marker.getTitle());
        } else {
            view.findViewById(R.id.title).setVisibility(View.GONE);
        }
        if (marker.getSnippet().length() > 0) {
            ((TextView) view.findViewById(R.id.snippet)).setText(marker.getSnippet().length());
        } else {
            view.findViewById(R.id.snippet).setVisibility(View.GONE);
        }

    }


    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    /**
     * 定义接口，用于回调当前的定位信息
     */
    public interface LonLatListener {
        void onLocationChanged(AMapLocation aMapLocation);

    }

}
